package com.zwb.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwb.boot.common.utils.RedisUtils;
import com.zwb.boot.dto.DishDto;
import com.zwb.boot.entity.Dish;
import com.zwb.boot.entity.DishFlavor;
import com.zwb.boot.mapper.DishMapper;
import com.zwb.boot.service.DishFlavorService;
import com.zwb.boot.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 头发又黑又长
 * @Date 2022/7/22 15:24
 * @email zwb15083976291@163.com
 */
@Slf4j
@Service
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired(required = false)
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorService flavorService;

    @Autowired
    private RedisUtils redisUtils;

    @Value("${redisCache.dishCachePrefix}")
    private String dishCachePrefix;

    /**
     * 根据 cateory id 查询数量
     *
     * @param id
     * @return
     */
    @Override
    public Long countByCategoryId(Long id) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId, id);
        return this.count(queryWrapper);
    }

    /**
     * 新增菜品和口味
     *
     * @param dishDto
     */
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto);
        // 菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors().stream().map(dishFlavor -> {
            dishFlavor.setDishId(dishDto.getId());
            return dishFlavor;
        }).collect(Collectors.toList());

        // 批量添加
        flavorService.saveBatch(flavors);

        // 删除缓存
        redisUtils.del(dishCachePrefix + dishDto.getCategoryId());
    }

    /**
     * 通过id查询 菜品和菜品的口味*
     *
     * @param id
     * @return
     */
    @Override
    public DishDto findByIdWithFlavor(Long id) {
        DishDto result = new DishDto();
        Dish dish = dishMapper.selectById(id);
        BeanUtils.copyProperties(dish, result);
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, result.getId());
        result.setFlavors(flavorService.list(queryWrapper));
        return result;
    }

    /**
     * 更新菜品信息
     *
     * @param dishDto
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {


        dishMapper.updateById(dishDto);

        List<DishFlavor> collect = dishDto.getFlavors().stream().map(item -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        // 删除原有的口味数据
        LambdaQueryWrapper<DishFlavor> deleteWrapper = new LambdaQueryWrapper<>();
        deleteWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        flavorService.remove(deleteWrapper);
        flavorService.saveBatch(collect);

        // 删除缓存
        redisUtils.del(dishCachePrefix + dishDto.getCategoryId());
    }

    /**
     * 删除 菜品和口味数据
     *
     * @param id
     */
    @Override
    public void deleteWithFlavor(Long id) {
        // 删除缓存
        redisUtils.del(dishCachePrefix + this.getById(id).getCategoryId());

        // 删除菜品数据
        dishMapper.deleteById(id);
        // 删除对于的口味数据
        LambdaUpdateWrapper<DishFlavor> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(DishFlavor::getDishId, id);
        flavorService.remove(wrapper);


    }


    /**
     * 批量删除菜品和 口味数据
     *
     * @param ids
     */
    @Override
    public void deleteWithFlavors(List<Long> ids) {
        ids.stream().forEach(this::deleteWithFlavor);
    }


}
